package util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.TimeZone;

import org.apache.log4j.Logger;

import pojo.Evento;

public class Utility {
	
    public static final String PROP_SMTP_HOST = "mail.smtp.host";
    public static final String PROP_SMTP_AUTH = "mail.smtp.auth";
    public static final String PROP_SMTP_STARTTLS = "mail.smtp.starttls.enable";
    public static final String PROP_SMTP_PORT = "mail.smtp.port";   
    public static final String PROP_SMTP_USERNAME = "username";
    public static final String PROP_SMTP_PASSWORD = "password";
    public static final String PROP_SMTP_RECIPE = "recipe";
    public static final String PROP_SMTP_FROM = "from";   
    public static final String PROP_CONTENT_TYPE = "content_type";
    public static final TimeZone timeZone = TimeZone.getTimeZone("Europe/Berlin");
	public static SimpleDateFormat sdfData = new SimpleDateFormat("dd-MM-yyyy");
	public static SimpleDateFormat sdfDataXml = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
	public static SimpleDateFormat sdfOra = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat sdfOraXml = new SimpleDateFormat("HH:mm:ss");
	public static String MOD = "MOD";
	public static String DEL = "DEL";
	public static String ADD = "ADD";
	public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." +
			"[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +
			"(\\.[A-Za-z]{2,})$";

	public static void confirmDirectory(File directory) {
		if ( ! directory.isDirectory() ) {
		    System.out.println( "\nError. Directory " + directory.getPath() +
					" does not exist.");
		    System.out.println( "       -p must point to the xmlData directory.");
	    
		}
    }	
	public static String delSpaces(String toDel){
		
		return toDel.replace(' ', '-');
	}
    public static String toString(Date data) {
		if ( data!=null ) 
		    return sdf.format(data);
		else
			return "";
    }
	
	
    public static Date toDate(String data) {
		
			try {
				return sdf.parse(data);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				return null;
			}
    }
    
    public static String toStringData(Date data) {
  		if ( data!=null ) 
  		    return sdfData.format(data);
  		else
  			return "";
      }
  	
  	
      public static Date toDateData(String data) {
  		
  			try {
  				return sdfData.parse(data);
  			} catch (ParseException e) {
  				// TODO Auto-generated catch block
  				//e.printStackTrace();
  				return null;
  			}
      }
    
	public static int toInt(String num) {
		
		try {
			return Integer.parseInt(num);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return -1;
		}			
    }
	public static String toString(int num) {
		
		try {
			return Integer.toString(num);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return "";
		}			
    }	
    public static String toStringOra(Date data) {
		if ( data!=null ) 
		    return sdfOra.format(data);
		else
			return "";
    }
	
	
    public static Date toDateOra(String data) {
		
			try {
				return sdfOra.parse(data);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				return null;
			}
    }
    public static String toStringOraXml(Date data) {
		if ( data!=null ) 
		    return sdfOraXml.format(data);
		else
			return "";
    }
	
	
    public static Date toDateOraXml(String data) {
		
			try {
				return sdfOraXml.parse(data);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				return null;
			}
    }
    public static String toStringDataXml(Date data) {
  		if ( data!=null ) 
  		    return sdfDataXml.format(data);
  		else
  			return "";
      }
  	
  	
      public static Date toDateDataXml(String data) {
  		
  			try {
  				return sdfDataXml.parse(data);
  			} catch (ParseException e) {
  				// TODO Auto-generated catch block
  				//e.printStackTrace();
  				return null;
  			}
      }

}
